<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "gky_dept".
 *
 * @property string $id
 * @property string $dname
 * @property string $serverid
 * @property string $parentserverid
 * @property string $operate
 */
class Dept extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'gky_dept';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['serverid', 'parentserverid'], 'integer'],
            [['dname'], 'string', 'max' => 128],
            [['operate'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'dname' => '部门名称',
            'serverid' => '服务编号',
            'parentserverid' => '父级服务编号',
            'operate' => '操作',
        ];
    }

    public static function getDept() {
        //联合表gky_dept和管gky_user查询
        $sql = "(SELECT * FROM gky_dept)
                    UNION
                (SELECT gky_user.id,gky_user.username as dname,'0',gky_user.parentserverid as parentserverid,'keep' FROM gky_user)";

        $depts = Dept::findBySql($sql)->all();

        $array = \yii\helpers\ArrayHelper::toArray($depts);
     
        foreach ($array as $value) {
            $array2['serverid'] = $value['serverid'];
            $array2['parentserverid'] = $value['parentserverid'];
            $array2['label'] = $value['dname'];
            if ($value['serverid'] == 0) {
                $array2['url'] = "index.php?r=gky-user%2Fview&id=" . $value['id'];
                $array2['userid']=$value['id'];
            }

            if ($value['parentserverid'] == 1 || $value['parentserverid'] == 0) {
                //设置第一个头结点
                $array2['active'] = true;
            } else {
                $array2['active'] = false;
            }

            $tree[] = $array2;
        }
        $tree = self::list_to_tree($tree);
        return $tree;
    }
    
    
    
//    public static function getDept2() {
//        //联合表gky_dept和管gky_user查询
////        $sql = "(SELECT * FROM gky_dept WHERE parentserverid != 0)
////                    UNION
////                (SELECT gky_user.id,gky_user.username as dname,'1',gky_user.parentserverid as parentserverid,'keep' FROM gky_user)";
////
////        $depts = Dept::findBySql($sql)->all();
//        
//      $depts=  Dept::find()->all();
//
//        $array = \yii\helpers\ArrayHelper::toArray($depts);
//     
//        foreach ($array as $value) {
//            $array2['serverid'] = $value['serverid'];
//            $array2['parentserverid'] = $value['parentserverid'];
//            $array2['label'] = $value['dname'];
////            if ($value['serverid'] == 1) {
////                $array2['url'] = "index.php?r=gky-user%2Fview&id=" . $value['id'];
////            }
////
//            if ($value['parentserverid'] == 1 ) {
//                //设置第一个头结点
//                $array2['active'] = true;
//            } else {
//                $array2['active'] = false;
//            }
//
//            $tree[] = $array2;
//        }
//        $tree = self::list_to_tree($tree);
//        return $tree;
//    }

    //以下拷贝自OneThink的TreeModel类
    public static function list_to_tree($list, $pk = 'serverid', $pid = 'parentserverid', $child = 'items', $root = 0) {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = & $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] = & $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent = & $refer[$parentId];
                        $parent[$child][] = & $list[$key];
                    }
                }
            }
        }
        return $tree;
    }

    /**
     * 关联获奖表
     *
     * @return \yii\db\ActiveQuery
     */
    public function getPrizes(){

        return $this->hasMany(Prize::className(),['prizedept'=>'id']);

    }
    
    
     /**
     * @return \yii\db\ActiveQuery
     */
    public function getParent() {
        return $this->hasOne(Dept::className(), ['id' => 'parentserverid']);
    }

//    /**
//     * @return \yii\db\ActiveQuery
//     */
//    public function getChildren() {
//        return $this->hasMany(Dept::className(), ['parentserverid' => 'id']);
//    }

    

}
